<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Swagger文档登录页面</title>
    <style type="text/css">
        body{background:url("images/login_bg.jpg") no-repeat;background-size:100%}
        #login_frame{width:29.57vw;height:19.23vw;padding:13px;background-color:rgba(240,255,255,0.5);border-radius:10px;margin:8% auto 0}
        form p{text-align:center}
        form .title{font-size:22px;font-weight:bold;color:#57c534;margin-bottom:12%}
        form .item{text-align:center;margin-bottom:10%}
        form p > *{display:inline-block;vertical-align:top}
        .label_input{font-size:16px;width:20%;height:35px;line-height:35px;text-align:center;color:white;background-color:#3CD8FF;border-top-left-radius:5px;border-bottom-left-radius:5px}
        .text_field{width:70%;height:35px;border-top-right-radius:5px;border-bottom-right-radius:5px;border:0;padding:0;text-align:center;font-size:16px}
        #btn_login{font-size:16px;width:50%;height:35px;line-height:35px;text-align:center;color:white;background-color:#3BD9FF;border-radius:6px;border:0;display:block;margin:0 auto}
        #login_control{display:block;width:100%}
    </style>
</head>
<body>
    <div id="login_frame">
        <form>
            <p class="title">Swagger Api文档</p>
            <p class="item"><label class="label_input">查看密码</label><input type="password" id="password" class="text_field"/></p>
            <div id="login_control">
                <input type="button" id="btn_login" value="查看文档" onclick="login()"/>
            </div>
        </form>
    </div>
    <script type="text/javascript" src="js/md5.js"></script>
    <script type="text/javascript">
        function login() {
            var pwd = document.getElementById('password').value;
            if (pwd.replace(/(^\s*)|(\s*$)/g,'') == '') {
                alert('查看密码不能为空');
                return false;
            }
            var date = new Date();
            //7天后cookie过期，需重新输入密码
            date.setTime(date.getTime() + 7*24*3600*1000);
            document.cookie = 'swagger_auth=' + hex_md5(pwd) + ';expires=' + date.toGMTString() + ';Path=/';
            window.location.href = '../../swagger-ui.html'
        }
        window.onload = function() {
            var getCookie = document.cookie.replace(/[ ]/g,'');
            var arrCookie = getCookie.split(';');
            for (var i=0; i<arrCookie.length; i++) {
                var arr=arrCookie[i].split('=');
                if ('swagger_auth' == arr[0]) {
                    if ('fail' == arr[1]) {
                        alert('密码错误');
                    }
                    break;
                }
            }
        }
        document.onkeydown = cdk ;
        function cdk(){
            if(event.keyCode ==13){
                login();
            }
        }
    </script>
</body>
</html>
